package cn.bravolinks.tools.controller;

import cn.bravolinks.tools.constant.Constant;
import cn.bravolinks.tools.domain.Task;
import cn.bravolinks.tools.domain.User;
import cn.bravolinks.tools.service.TaskService;
import cn.bravolinks.tools.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("task")
public class TaskController extends BaseController{

    @Autowired
    private TaskService taskService;
    @Autowired
    private UserService userService;
    /**
     * 查询任务
     * @return
     */
    @RequestMapping("/")
    public String loginPage(String menuType){
    	User u = (User) session.getAttribute(Constant.LOGIN_USER);
    	List<User> userList =userService.queryUserByType("产品");
        request.setAttribute("user",u);
        request.setAttribute("userList",userList);
        request.setAttribute("menuType",menuType);
        //查询该用户的任务
        List<Task> tasks = taskService.get(u.getId(),null,null);
        request.setAttribute("tasks",tasks);
        return "TaskManagement";
    }
    /**
     * 结束任务
     * @param task
     * @return
     */
    @RequestMapping("endTask")
    @ResponseBody
    public int endTask(Task task){
        task.setFinalEndTime(new Date());
        return taskService.endTask(task);
    }

    /**
     * 添加一条记录
     * @param task
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public int insert(@RequestBody Task task){
        return taskService.insert(task);
    }
}
